package com.yf.words.controller;

import com.yf.words.service.TagService;
import com.yf.words.service.WordService;
import com.yf.words.util.Result;
import com.yf.words.util.ResultGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@CrossOrigin("*")
@RequestMapping("words")
public class WordsController {

    @Autowired
    WordService wordService;

    @Autowired
    TagService tagService;

    /**
     * 单词列表
     * @return
     */
    @GetMapping
    public Result index() {
        return ResultGenerator.genSuccessResult(wordService.getAllWords());
    }

    @GetMapping("{word}")
    public Result word(@PathVariable String word) {
        Map<String, Object> ret = new HashMap<>();
        ret.put("wordDetails", wordService.getDataByWord(word));
        ret.put("sentences", wordService.getSentencesByWord(word));
        ret.put("tags",  tagService.getTagsByWord(word));
        return ResultGenerator.genSuccessResult(ret);
    }
}